Java String
In java, string is a collection of characters that is enclosed in double quotes.
Example
string user = "Andrew Rayan";
Example for string program
public class Main
{
public static void main(String[] args) {
String language = "Java";
// print strings
System.out.println(language); // print Java
}
}
Output
Java
length() method
To get the length of the string we use the length()
method.
Example for getting string length
public class Main
{
public static void main(String[] args) {
String language = "Java";
// print strings
System.out.println(language); // print Java
System.out.println("Length of the string is "+language.length());
}
}
Output
Java
4
concat() method
The concat()
method is used to join two strings.
Example to join two strings
public class Main
{
public static void main(String[] args) {
String first = "Java ";
String second = "Programming";
System.out.println(first.concat(second));
}
}
Output
Java Programming
equals() method
The equals()
method is used to compare two strings. If two strings are equal, then it will return true
else it will return false
.
Example for comparing two strings
public class Main
{
public static void main(String[] args) {
String firstLanguage = "Java";
String secondLanguage = "C Programming";
String thirdLanguage = "Java";
System.out.printf("%s and %s are %s",firstLanguage,secondLanguage,firstLanguage.equals(secondLanguage));
System.out.println();
System.out.printf("%s and %s are %s",firstLanguage,thirdLanguage,firstLanguage.equals(thirdLanguage));
}
}
Output
Java and C Programming are false
Java and Java are true
contains() method
The contains()
method is used to check a particular string is available in another string. It will return true
if the string is available in other string, else it will return false
.
Example for contains() method
public class Main
{
public static void main(String[] args) {
String firstLanguage = "Java";
String secondLanguage = "Java Programming";
boolean isAvailable = secondLanguage.contains(firstLanguage);
if(isAvailable){
System.out.printf("%s is present in %s",secondLanguage,firstLanguage);
} else{
System.out.printf("%s is not present in %s",secondLanguage,firstLanguage);
}
}
}
Output
Java Programming is present in Java
In the above program, we check whether the secondLanguage
variable contains the firstLanguage
variable value. In the above case, it will return true
.
substring() method
The substring()
method is used to get the value from the string by specifying the start and end index position.
If only one index position is given, then it will take value from that index position to the end of the string.
Example for substring() method
public class Main {
public static void main(String[] args) {
String str1 = "Java program";
// string from first character to the end
System.out.println(str1.substring(0)); // Java program
// string from 4th character to the end
System.out.println(str1.substring(3)); // a program
// string from 0th character to 4th character
System.out.println(str1.substring(0,4)); // Java
}
}
Output
Java program
a program
Java
join() method
The join()
method is used to join multiple strings into a single string with the specified delimiter.
Example for join() method
class Main {
public static void main(String[] args) {
String str1 = "I";
String str2 = "love";
String str3 = "Java";
String str4 = "Programming";
// join strings with space between them
String result = String.join(" ", str1, str2, str3,str4);
System.out.println(result);
}
}
Output
I love Java Programming
In the above program, the first parameter space(" "
) is the delimiter, which will join str1
,str2
and str3
and stores the result in result
variable.
replace() method
The replace()
method is used to replaces the first parameter character value with the second parameter character value.
Example for replace() method
class Main {
public static void main(String[] args) {
String str1 = "Java programs";
// replace the character a with *
System.out.println(str1.replace('a', '*'));
}
}
Output
J*v* progr*ms
replaceAll() method
The replaceAll()
method takes 2 parameters, the first one is the string to be replaced and it can be a regex pattern. The second parameter is the string that will replace the first parameter value.
Example for replaceAll() method
class Main {
public static void main(String[] args) {
String str = "I123like456Java2424Programming";
String text = "I like C#";
String regex = "\\d+";
// replace all numeric digits with a space
System.out.println(str.replaceAll(regex, " "));
System.out.println(text.replaceAll("C#", "Java"));
}
}
Output
I like Java Programming
I like Java
In the above program, we have replaced all the digits in str
variable with a space using the regex
pattern.
Then, in the text
variable, we have replaced the C#
with Java
value.
indexOf() method
The indexOf()
method is used to find the index position of the string or a character in a string variable. It always returns the first occurance index position.
If the string or the character is not present in the string variable, then it will return -1
.
Example for indexOf() method
class Main {
public static void main(String[] args) {
String str = "I like Java";
System.out.println("Index position of 'e' is "+str.indexOf('e'));
System.out.println("Index position of 'like' is "+str.indexOf("like"));
System.out.println("Index position of 'c#' is "+str.indexOf("c#"));
}
}
Output
Index position of 'e' is 5
Index position of 'like' is 2
Index position of 'c#' is -1
In the above program, we try to find the index position of character e
and the string like
and c#
.
If the index position is not found then it will return -1
. Here c#
is not present in the str
variable so it returns -1
.
charAt() method
The charAt()
method is used to get the character at particular index position.
Example for charAt() method
class Main {
public static void main(String[] args) {
String string1 = "I like Java";
System.out.println("Character at index position 5 is "+string1.charAt(5));
}
}
Output
Character at index position 5 is e
trim() method
The trim()
method is used to trim the empty space before and after the string.
Example of trim() method
class Main {
public static void main(String[] args) {
String str = " I like Java ";
System.out.println("Before trimming:"+str.length());
System.out.println(str.trim());
System.out.println("After trimming:"+str.trim().length());
}
}
Output
Before trimming:17
I like Java
After trimming:11
In the above program, we have used the trim()
method to remove the empty space in the str
variable and prints its value.
isEmpty() method
The isEmpty()
method is used to find out whether the string is empty or not.
Example for isEmpty() method
class Main {
public static void main(String[] args) {
String string1 = "I like Java";
String string2 = "";
System.out.println(string1.isEmpty()); // false
System.out.println(string2.isEmpty()); // true
}
}
Output
false
true
split() method
The split()
method is used to split the string into array of string based on the parameter. The parameter is of string data type.
Example for split() method
class Main {
public static void main(String[] args) {
String string1 = "I like Java";
String[] resultArray = string1.split(" ");
for(int i=0;i<resultArray.length;i++){
if(i==resultArray.length-1){
System.out.print(resultArray[i]);
}else{
System.out.print(resultArray[i] + ", ");
}
}
}
}
Output
I, like, Java
In the example program, we split the string1
variable based on the space and store it in resultArray
string array.
Later, we loop through the resultArray
variable and print the result in the string array.